<?php

namespace app\admin\controller\mall\order;

use app\admin\controller\mall\Base;

/**
 * 订单
 *
 * @icon fa fa-circle-o
 */
class Order extends Base
{
    
    /**
     * Order模型对象
     * @var \app\common\model\mall\order\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\order\Order;
        $this->view->assign("delivertypeList", $this->model->getDelivertypeList());
        $this->view->assign("paytypeList", $this->model->getPaytypeList());
        $this->view->assign("ordersrcList", $this->model->getOrdersrcList());
        $this->view->assign("promTypeList", $this->model->getPromTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 订单详情
     */
    public function detail($ids = NULL){
        if(!$ids)$ids=$this->request->param("ids");
        if ($this->request->isAjax()) {
            //获取订单商品
            $list = model('\app\common\model\mall\order\Goods')->alias('og')
                ->join('__MALL_GOODS_GOODS__ g','og.goods_id=g.id','left')
                ->join('__MALL_SHOP_SHOP__ s','g.shop_id=s.id','left')
                ->where('order_id',$ids)
                ->field('og.*,og.goodsPrice*og.goodsNum subtotal,g.goodsSn,s.areaIds shopAreaIds,s.shopAddress,s.shopTel,s.name,s.shopQQ,s.shopWangWang')
                ->order('id asc')->select();

            $result = array("total" => count($list), "rows" => $list);
            return json($result);
        }
        $row = $this->model->detail($ids);
        if (!$row) $this->error(__('No Results were found'));
        $this->assign('row',$row);
        return $this->fetch();
    }

    /**
     * 订单打印
     */
    public function printf($ids = NULL){
        $row = $this->model->detail($ids);
        if (!$row) $this->error(__('No Results were found'));
        $this->assign('row',$row);

        //获取订单商品
        $list = db('mall_order_goods')->alias('og')
            ->join('__MALL_GOODS_GOODS__ g','og.goods_id=g.id','left')
            ->join('__MALL_SHOP_SHOP__ s','g.shop_id=s.id','left')
            ->where('order_id',$ids)
            ->field('og.*,og.goodsPrice*og.goodsNum subtotal,g.goodsSn,s.areaIds shopAreaIds,s.shopAddress,s.shopTel,s.name,s.shopQQ,s.shopWangWang')
            ->order('id asc')->select();

        // 获取店铺地址
        foreach($list as $k=>$v){
            $areaNames = model('\app\common\model\mall\oper\Area')
                ->where("id","in",$v['shopAreaIds'])->column("name");
            $list[$k]['shopAddress'] = implode('',$areaNames).$v['shopAddress'];
        }
        $this->assign('list',$list);

        return $this->fetch();
    }
}
